package edu.nf.exam.controller;

import com.github.pagehelper.PageInfo;
import edu.nf.exam.entity.PaperQuestionInfo;
import edu.nf.exam.service.PaperQuestionService;
import edu.nf.exam.service.impl.PaperQuestionServiceImpl;
import org.nf.mvc.annotation.RequestMapping;
import org.nf.mvc.servlet.View;
import org.nf.mvc.view.JsonView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author : LLH
 * @create 2021/6/25
 */
public class PaperQuestionController extends BaseController{

    /**
     * 查询
     * @param paperQuestionInfo
     * @return
     */
    @RequestMapping("/paperQuestion/select")
    public View select(PaperQuestionInfo paperQuestionInfo){
        PaperQuestionService service=new PaperQuestionServiceImpl();
        try {
            List<PaperQuestionInfo> list = service.checkSelect(paperQuestionInfo);
            return new JsonView(success(list));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }

    }
    /**
     * 添加
     * @param paperQuestionInfo
     * @return
     */
    @RequestMapping("/paperQuestion/add")
    public View add(PaperQuestionInfo paperQuestionInfo){
        PaperQuestionService service=new PaperQuestionServiceImpl();
        try {
            service.checkAdd(paperQuestionInfo);
            return new JsonView(success("添加成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }

    }

    /**
     * 修改
     * @param paperQuestionInfo
     * @return
     */
    @RequestMapping("/paperQuestion/update")
    public View update(PaperQuestionInfo paperQuestionInfo){
        PaperQuestionService service=new PaperQuestionServiceImpl();
        try {
            service.checkUpdate(paperQuestionInfo);
            return new JsonView(success("修改成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     * 分页查询
     * @param paperQuestionId
     * @param difficult
     * @param questionType
     * @param levelName
     * @param subjectName
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/paperQuestion/selectByPage")
    public View selectByPage(Integer paperQuestionId,Integer difficult, Integer questionType, String levelName, String subjectName,Integer page,Integer limit){
        PaperQuestionService service=new PaperQuestionServiceImpl();
        PageInfo<PaperQuestionInfo> pageInfo = service.selectByPage(paperQuestionId,difficult, questionType, levelName, subjectName, page, limit);

        JsonView jsonView = new JsonView(page(pageInfo));
        jsonView.setDateFormat("yyyy-MM-dd HH:mm:ss");
        return jsonView;
    }


    /**
     * 根据ID查询
     * @return
     */
    @RequestMapping("/paperQuestion/selectById")
    public View selectById(HttpServletRequest request){
        // 获取试卷的试题编号
        String questions = request.getParameter("questions");

        // 用于接收试题数据
        List<PaperQuestionInfo> list=new ArrayList<>();
        // 用于统计单选题数量
        int singleCount=0;
        // 用于统计多选题数量
        int multipleCount=0;
        // 用于统计判断题数量
        int judgeCount=0;

        try {
            // 存放所有试题ID
            String[] arr = questions.split("&");
            PaperQuestionService service=new PaperQuestionServiceImpl();
            // 循环遍历
            for (String a : arr) {
                // 根据试题ID查询试题数据
                Integer paperQuestionId= Integer.parseInt(a);
                PaperQuestionInfo paperQuestionInfo = service.selectById(paperQuestionId);
                // 判断其题型
                if(paperQuestionInfo.getQuestionType()==1){
                    singleCount++;
                }else if(paperQuestionInfo.getQuestionType()==2){
                    multipleCount++;
                }else if(paperQuestionInfo.getQuestionType()==3){
                    judgeCount++;
                }
                list.add(paperQuestionInfo);
            }

            JsonView jsonView = new JsonView(success(list,singleCount,multipleCount,judgeCount));
            jsonView.setDateFormat("yyyy-MM-dd HH:mm:ss");
            return jsonView;

        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }

    }


    @RequestMapping("/paperQuestion/selectActiceDegree")
    public View selectMonthCount(HttpServletRequest request){
        String[] dateArrs = request.getParameterValues("dateArr");

        PaperQuestionService service=new PaperQuestionServiceImpl();
        // 定义一个空数组，用于接收本月每天的题目数量
        int[] arr=new int[dateArrs.length];

        try {
            for (int i=0;i< dateArrs.length;i++) {
                List<PaperQuestionInfo> list = service.selectMonthCount(dateArrs[i]);
                arr[i]=list.size();
            }
            return new JsonView(success(arr));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,"查询失败"));
        }
    }
}
